var flyout_htmlDetailsView;
var flyout_cssString = "", flyout_jsString = "";
var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
function GetProgramDescription(id, prog) {
	if (!framework.system.network.online || g_Feed.channels[id].program.length == 0 || typeof g_Feed.channels[id].program[prog] == "undefined") {
		opendetails("Pausefisk", strings.NOT_CONNECTED_DETAILS);
		return;
	}
	var state = options.getValue("TVTID.onfly.Openstate");
	if (state == "") {
		alert("Programbeskrivelsens vindue huskes og vises bedst ved minimum dimension p\xC3\xA5 600x400.\nTr\xC3\xA6k i resizehandle't og tilpas dine \xC3\xB8nsker.");
		options.putValue("TVTID.onfly.Openstate", "initial");
	} else if (state == "initial") {
		if (!confirm("Husk at st\xC3\xB8rrelsen p\xC3\xA5 programbeskrivelsens vindue huskes og vises bedst ved minimum dimension p\xC3\xA5 600x400.\n\nV\xC3\xA6lg nej hvis du ikke \xC3\xB8nsker denne p\xC3\xA5mindelse igen.")) {
			options.putValue("TVTID.onfly.Openstate", "done");
		}
	}
	var buffer;
	var http = new XMLHttpRequest;
	if (g_Feed.channels[id].name.toLowerCase().match("showtime") && g_Feed.channels[id].program[prog].start_timestamp * 1000 < (new Date).getTime() && g_Feed.channels[id].program[prog].end_timestamp * 1000 > (new Date).getTime()) {
		http.open("GET", "http://ontv.dk/tv/182", false);
		http.send(null);
		var b = http.responseText.indexOf("<div class=\"marked\">");
		if (b != -1) {
			b = http.responseText.indexOf("<p", b);
			if (b != -1) {
				var e = http.responseText.indexOf("</p>", b + 3) + "</p>".length;
			}
		}
		if (typeof e != "undefined") {
			var ontv = http.responseText.substring(b, e);
		}
	}
	var url = "http://tvtid.tv2.dk/program/index.php/id-" + g_Feed.channels[id].program[prog].program_id + ".html";
	http.open("get", url, true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			if (http.status == 200) {
				buffer = http.responseText.substring(http.responseText.indexOf("<div class=\"programPage\">"), http.responseText.indexOf("<!-- START TNS Metrix -->"));
				while (buffer.indexOf("src=\"/js/") != -1) {
					buffer = buffer.replace("src=\"/js/", "src=\"http://tvtid.tv2.dk/js/");
				}
				buffer = buffer.replace(/\[[0-9]*\]/, "");
				buffer = buffer.replace("<div class=\"programLeft\"> </div>", "");
				if (typeof ontv != "undefined") {
					buffer += "<script type=\"text/javascript\">var ontv = '" + ontv.replace(/\'/g, "\\'") + "';</script>";
				} else if (g_Feed.channels[id].name.toLowerCase().match("showtime")) {
					buffer += "<p><h2 class=\"programListHeader\">Bes\xC3\xB8g ONTV for en ikke-trunkeret-programbeskrivelse</h2><a href=\"http://ontv.dk/tv/182\">@NTV</a></p>";
				}
				log(buffer);
				opendetails(g_Feed.channels[id].program[prog].title, buffer);
			}
		}
	};

	http.send(null);
}

function opendetails(__title, __detailsHTML, __onclose, __externalFunctions) {
	if (flyout_cssString == "") {
		flyout_cssString = storage.openText("details_css.css");
	}
	if (!/upcoming/i.test(__title) && flyout_jsString == "") {
		flyout_jsString = storage.openText("details_js.js");
	}
	flyout_onCloseView = typeof __onclose == "function" ? __onclose : function() { };
	plugin.closeDetailsView();
	flyout_htmlDetailsView = new DetailsView;
	flyout_htmlDetailsView.html_content = true;
	flyout_htmlDetailsView.setContent("", undefined, "<html>" + "<head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />" + "<script language=\"Javascript\">" + "  document.oncontextmenu=new Function(\"return false\")" + "</script>" + "<style type=\"text/css\">" + flyout_cssString + "</style>" + "</head><body>" + "<div id=\"flyoutContentFrame\">" + __detailsHTML + "</div>" + "<script type=\"text/javascript\"\">" + flyout_jsString + "</script>" + "</body></html>", false, 0);
	flyout_htmlDetailsView.external = {};
	var __externalObject = new Object();
	__externalObject.openUrl = function(url) {
		framework.openUrl( url );
	};
	__externalObject.close = function() {
		plugin.closeDetailsView();
	};
	__externalObject.runUrl = function(url) {
		alert(url)
		new ActiveXObject("WScript.Shell").run(url);
	}
	for(var fkt in __externalFunctions) {
		__externalObject[fkt] = __externalFunctions[fkt];
	}
	flyout_htmlDetailsView.external = __externalObject;
	
	plugin.showDetailsView(flyout_htmlDetailsView, __title, gddDetailsViewFlagNone, on_detailsViewFeedback);

}
var flyout_onCloseView = function () {};
function on_detailsViewFeedback(detailsViewFlags) {
	if (detailsViewFlags == gddDetailsViewFlagNone) {
		flyout_onCloseView();
	} else if (detailsViewFlags == gddDetailsViewFlagToolbarOpen) {}
	flyout_htmlDetailsView = null;
	CollectGarbage();
}
